﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string CardBind_Add(CardBind model ,int ACID);


[OperationContract]
string CardBind_Update(CardBind model ,int ACID);


[OperationContract]
string CardBindBybid_Delete(int bid,int ACID);


[OperationContract]
CardBind CardBindBybid_Get(int bid,int ACID );


[OperationContract]
List<CardBind> CardBind_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加CardBind表数据
/// <summary>
/// 添加CardBind
/// </summary>
/// <param name="model">CardBind实体</param>
/// <returns>空或错误</returns>
public string CardBind_Add(CardBind model,int ACID)
{
if(model.bid==null )
{
return "『ID』不能为空！";
}
if(model.sdate ==null )
{
return "『绑定生效日期』不能为空！";
}
if(model.ComboID==null )
{
return "『套餐ID』不能为空！";
}
if(string.IsNullOrEmpty(model.cno))
{
return "『串号』不能为空！";
}
if(string.IsNullOrEmpty(model.phone))
{
return "『电话号码』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.CardBind.Count(p=>p.bid==model.bid)>0)
{
return "主键已存在！";
}
ctx.CardBind.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加CardBind表数据事物用重构
/// <summary>
/// 添加CardBind事物用重构
/// </summary>
/// <param name="model">CardBind实体</param>
/// <returns>空或错误</returns>
private string CardBind_Add(CardBind model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(model.bid==null )
{
return "『ID』不能为空！";
}
if(model.sdate ==null )
{
return "『绑定生效日期』不能为空！";
}
if(model.ComboID==null )
{
return "『套餐ID』不能为空！";
}
if(string.IsNullOrEmpty(model.cno))
{
return "『串号』不能为空！";
}
if(string.IsNullOrEmpty(model.phone))
{
return "『电话号码』不能为空！";
}
if(ctx.CardBind.Count(p=>p.bid==model.bid)>0)
{
return "主键已存在！";
}
ctx.CardBind.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改CardBind表数据
/// <summary>
/// 修改CardBind
/// </summary>
/// <param name="model">CardBind实体</param>
/// <returns>空或错误</returns>
public string CardBind_Update(CardBind model,int ACID)
{
if(model.bid==null )
{
return "『ID』不能为空！";
}
if(model.sdate ==null )
{
return "『绑定生效日期』不能为空！";
}
if(model.ComboID==null )
{
return "『套餐ID』不能为空！";
}
if(string.IsNullOrEmpty(model.cno))
{
return "『串号』不能为空！";
}
if(string.IsNullOrEmpty(model.phone))
{
return "『电话号码』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.CardBind.Count(p=>p.bid==model.bid)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
CardBind CardBindmodel=ctx.CardBind.Single(p => p.bid==model.bid);
if(model.bid!=null )
{
CardBindmodel.bid=model.bid;
}
if(!string.IsNullOrEmpty(model.phone))
{
CardBindmodel.phone=model.phone;
}
if(!string.IsNullOrEmpty(model.cno))
{
CardBindmodel.cno=model.cno;
}
if(model.ComboID!=null )
{
CardBindmodel.ComboID=model.ComboID;
}
if(model.sdate!=null )
{
CardBindmodel.sdate=model.sdate;
}
if(model.edate!=null )
{
CardBindmodel.edate=model.edate;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除CardBind表Bybid数据
/// <summary>
/// 删除CardBindBybid
/// </summary>
/// <param name="bid">ID</param>
/// <returns>空或错误</returns>
public string CardBindBybid_Delete(int bid,int ACID)
{
if(bid==null )
{
return "『ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.CardBind.Count(p=>p.bid==bid)!=1)
{
return null;
}
CardBind model=ctx.CardBind.Single(p => p.bid== bid);
ctx.CardBind.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除CardBind表Bybid数据[事物用重构]
/// <summary>
/// 删除CardBindBybid[事物用重构]
/// </summary>
/// <param name="bid">ID</param>
/// <returns>空或错误</returns>
private string CardBindBybid_Delete(int bid,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(bid==null )
{
return "『ID』不能为空！";
}
if(ctx.CardBind.Count(p=>p.bid==bid)!=1)
{
return null;
}
CardBind model=ctx.CardBind.Single(p => p.bid== bid);
ctx.CardBind.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取CardBind表Bybid数据
/// <summary>
/// 获取CardBindBybid
/// </summary>
/// <param name="bid">ID</param>
/// <returns>当前表</returns>
public CardBind CardBindBybid_Get(int bid,int ACID)
{
if(bid==null )
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.CardBind.Count(p=>p.bid==bid)!=1)
{
return null;
}
return ctx.CardBind.SingleOrDefault(p=>p.bid==bid);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取CardBind表数据
/// <summary>
/// 获取CardBind
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<CardBind></returns>
public List<CardBind> CardBind_Get(Expression<Func<CardBind, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.CardBind.ToList();
}
 else 
{
return ctx.CardBind.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.CardBind.Where(predicate).ToList();
}
 else 
{
return ctx.CardBind.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取CardBind表数据
/// <summary>
/// 获取CardBind
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<CardBind></returns>
public List<CardBind> CardBind_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return CardBind_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取CardBind表数据计数
/// <summary>
/// 获取CardBind表数据计数
/// </summary>
/// <returns>List<CardBind></returns>
public int CardBind_GetCount(Expression<Func<CardBind, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.CardBind.Count();

}
 else
{
return ctx.CardBind.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取CardBind分页表数据
/// <summary>
/// 获取CardBind分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<CardBind></returns>
public List<CardBind> CardBind_Getpage(int pageIndex, int pageSize,Expression<Func<CardBind, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.CardBind.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.CardBind.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.CardBind.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.CardBind.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改CardBind表数据
/// <summary>
/// 批量修改CardBind
/// </summary>
/// <param name="predicate">Expression<Func<CardBind, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<CardBind,CardBind>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int CardBind_batchUpdate(Expression<Func<CardBind, bool>> predicate,Expression<Func<CardBind, CardBind>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.CardBind.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改CardBind表数据事物用重构
/// <summary>
/// 批量修改CardBind事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<CardBind, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<CardBind,CardBind>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int CardBind_batchUpdate(Expression<Func<CardBind, bool>> predicate,Expression<Func<CardBind, CardBind>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.CardBind.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除CardBind表数据
/// <summary>
/// 批量删除CardBind
/// </summary>
/// <param name="predicate">Expression<Func<CardBind, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int CardBind_batchDel(Expression<Func<CardBind, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.CardBind.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除CardBind表数据事物用重构
/// <summary>
/// 批量删除CardBind事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<CardBind, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int CardBind_batchDel(Expression<Func<CardBind, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.CardBind.Delete(predicate);
return ok;
}
#endregion


}
}

